package com.atguigu.edustatistics.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.edustatistics.entity.StatisticsDaily;
import com.atguigu.edustatistics.feign.UcenterApi;
import com.atguigu.edustatistics.mapper.StatisticsDailyMapper;
import com.atguigu.edustatistics.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.bouncycastle.pqc.math.linearalgebra.RandUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Cyrus
 * @since 2020-12-19
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterApi ucenterApi;

    @Override
    public void registerCount(String day) {
        //添加记录之前先删除相同日期数据
        baseMapper.delete(new QueryWrapper<StatisticsDaily>().lambda().eq(StatisticsDaily::getDateCalculated, day));


        //远程调用获取count值
        R registerR = ucenterApi.getRegister(day);
        Integer count = (Integer) registerR.getData().get("count");

        //设置参数
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        statisticsDaily.setRegisterNum(count);
        statisticsDaily.setDateCalculated(day);
        statisticsDaily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setLoginNum(RandomUtils.nextInt(100, 200));
        statisticsDaily.setCourseNum(RandomUtils.nextInt(100, 200));

        baseMapper.insert(statisticsDaily);
    }

    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.lambda().between(StatisticsDaily::getDateCalculated, begin, end);
        wrapper.select("date_calculated",type);
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //设置返回的数据格式
        /**
         *  data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
         *  data: [820, 932, 901, 934, 1290, 1330, 1320],
         * 使用list返回json格式集合
         */
        ArrayList<String> dateCalculatedList = new ArrayList();
        ArrayList<Integer> numDataList = new ArrayList();

        //遍历所有数据进行封装list
        for (StatisticsDaily statisticsDaily : staList) {
            dateCalculatedList.add(statisticsDaily.getDateCalculated());
            switch (type) {
                case "login_num":
                    numDataList.add(statisticsDaily.getLoginNum());
                    break;
                case "register_num":
                    numDataList.add(statisticsDaily.getRegisterNum());
                    break;
                case "video_view_num":
                    numDataList.add(statisticsDaily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(statisticsDaily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap();
        map.put("dateCalculatedList", dateCalculatedList);
        map.put("numDataList", numDataList);
        return map;
    }
}
